package com.yg.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yg.item.feign.ImageFeignService;
import com.yg.item.pojo.Item;
import com.yg.item.mapper.ItemMapper;
import com.yg.item.pojo.ItemType;
import com.yg.item.pojo.vo.CategoryVo;
import com.yg.item.pojo.vo.HotItemVo;
import com.yg.item.pojo.vo.ItemVo;
import com.yg.item.pojo.vo.OneCategory;
import com.yg.item.service.CategoryService;
import com.yg.item.service.ItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yg.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 节目表 服务实现类
 * </p>
 *
 * @author 魏成龙
 * @since 2020-11-07
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
     private CategoryService categoryService;
    @Autowired
    private ImageFeignService imageFeignService;
    @Override
    public List<ItemVo> getAllCategory() {
        List<ItemVo> list = new ArrayList<>();
        List<OneCategory> allCategory = categoryService.getAllCategory();
        for (int i = 0; i < allCategory.size(); i++) {
            OneCategory oneCategory = allCategory.get(i);
            ItemVo itemVo = new ItemVo();
            itemVo.setId(oneCategory.getId());
            itemVo.setItemType(oneCategory.getItemType());
            itemVo.setAliasName(oneCategory.getAliasName());
            itemVo.setLevel(oneCategory.getLevel());
            itemVo.setParent(oneCategory.getParent());
            List<ItemType> children1 = oneCategory.getChildren();

            List<CategoryVo> children = new ArrayList<>();
            for (int i1 = 0; i1 < children1.size(); i1++) {
                ItemType itemType = children1.get(i1);
                CategoryVo categoryVo = new CategoryVo();
                categoryVo.setAliasName(itemType.getAliasName());
                categoryVo.setId(itemType.getId());
                categoryVo.setLevel(itemType.getLevel());
                categoryVo.setParent(itemType.getParent());
                categoryVo.setItemType(itemType.getItemType());
                children.add(categoryVo);
            }

            itemVo.setChildren(children);
            List<HotItemVo> hotItemVoList = getItemByItemType1Id(itemVo.getId());
            itemVo.setHotItems(hotItemVoList);
            list.add(itemVo);
        }

        return list;
    }

    @Override
    public List<HotItemVo> getRecommendList() {
        //获取所有推荐  is_recommend = 1
        QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
        itemQueryWrapper.eq("is_recommend",1);

        List<Item> items = baseMapper.selectList(itemQueryWrapper);

        //存储hotItemVo
        List<HotItemVo> list = new ArrayList<>();
        for (int i = 0; i < items.size(); i++) {
            Item item = items.get(i);
            HotItemVo hotItemVo = new HotItemVo();

            hotItemVo.setMinPrice(item.getMinPrice());
            hotItemVo.setId(item.getId());
            hotItemVo.setItemName(item.getItemName());
            Result imageUrl = imageFeignService.getImageUrl(item.getId());
            String data = (String) imageUrl.getData();

            hotItemVo.setImageUrl(data);
            list.add(hotItemVo);

        }
        return list;
    }

    @Override
    public List<HotItemVo> getOnSale() {
        //即将出售 state = 2
        QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
        itemQueryWrapper.eq("state",2);
        List<Item> items = baseMapper.selectList(itemQueryWrapper);
        List<HotItemVo> list = new ArrayList<>();
        for (Item item : items) {
            HotItemVo hotItemVo = new HotItemVo();
            hotItemVo.setItemName(item.getItemName());
            hotItemVo.setId(item.getId());
            hotItemVo.setMinPrice(item.getMinPrice());
            Result imageUrl = imageFeignService.getImageUrl(item.getId());
            String data = (String) imageUrl.getData();
            hotItemVo.setImageUrl(data);
            list.add(hotItemVo);
        }

        return list;
    }


    private List<HotItemVo> getItemByItemType1Id(Long id){
        QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
        itemQueryWrapper.eq("item_type1_id",id);
        List<Item> items = baseMapper.selectList(itemQueryWrapper);

        List<HotItemVo> list = new ArrayList<>();
        for (int i = 0; i < items.size(); i++) {
            HotItemVo hotItemVo = new HotItemVo();
            Item item = items.get(i);
            hotItemVo.setId(item.getId());
            hotItemVo.setItemName(item.getItemName());
            hotItemVo.setMinPrice(item.getMinPrice());

            Result result = imageFeignService.getImageUrl(item.getId());
            String data = (String) result.getData();
            hotItemVo.setImageUrl(data);

            list.add(hotItemVo);
        }
        return list;
    }
}
